Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flamingo Implementation #23063

Closed
wants to merge 4 commits into from
Closed

Flamingo Implementation #23063

wants to merge 4 commits into from

Conversation

king159
Copy link

@king159 king159 commented Apr 29, 2023

What does this PR do?

Implementation of Flamingo models (https://arxiv.org/abs/2204.14198). Model weights trained by Open Flamingo team can be downloaded here. Weight conversion script is included.

Weights conversion can be run via:

python src/transformers/models/flamingo/converting_flamingo_to_hf.py \
    --old_ckpt_path /path/to/open/flamingo/weights \
    --new_hf_path /output/path

Models can then be loaded via:

model = transformers.FlamingoForConditionalGeneration.from_pretrained("/output/path")

Example:

import requests
import torch
import transformers
from PIL import Image

tokenizer = model.text_tokenizer
image_processor = transformers.CLIPImageProcessor()
demo_image_one = Image.open(
    requests.get(
        "http://images.cocodataset.org/val2017/000000039769.jpg", stream=True
    ).raw
)
demo_image_two = Image.open(
    requests.get(
        "http://images.cocodataset.org/test-stuff2017/000000028137.jpg", stream=True
    ).raw
)
query_image = Image.open(
    requests.get(
        "http://images.cocodataset.org/test-stuff2017/000000028352.jpg", stream=True
    ).raw
)
vision_x = (
    image_processor.preprocess(
        [demo_image_one, demo_image_two, query_image], return_tensors="pt"
    )["pixel_values"]
    .unsqueeze(1)
    .unsqueeze(0)
)
model.text_tokenizer.padding_side = "left"
lang_x = tokenizer(
    ["<image>An image of two cats.<|endofchunk|><image>An image of a bathroom sink.<|endofchunk|><image>An image of"],
    return_tensors="pt",
)

generated_text = model.generate(
    vision_x=vision_x,
    lang_x=lang_x["input_ids"],
    attention_mask=lang_x["attention_mask"],
    max_new_tokens=20,
    num_beams=3,
)

print("Generated text: ", model.text_tokenizer.decode(generated_text[0]))

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@king159 king159 changed the title Add flamingo Flamingo Implementation Apr 29, 2023
@Luodian
Copy link

Luodian commented Apr 29, 2023

Respect! Openflamingo needs be built with huggingface transformers for more efficient training and inference.

We have already adapted it in our Otter model (an instruction tuned model based on flamingo). We uploaded a converted openflamingo-9b weights at luodian/openflamingo-9b-hf.

The model could be loaded via

model = transformers.FlamingoForConditionalGeneration.from_pretrained("luodian/openflamingo-9b-hf")

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@king159 king159 marked this pull request as draft April 29, 2023 13:52
@sgugger
Copy link
Collaborator

sgugger commented May 1, 2023

cc @amyeroberts and @younesbelkada

@younesbelkada
Copy link
Contributor

Awesome work! Let us know when the PR is ready for review!

@huggingface huggingface deleted a comment from github-actions bot Jun 2, 2023
@huggingface huggingface deleted a comment from github-actions bot Jun 27, 2023
@king159 king159 closed this by deleting the head repository Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants